package shiyl.use.springboot.requestparam.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import static java.lang.String.format;
import static shiyl.use.springboot.requestparam.controller.ControllerHelper.createResponseMap;
import static shiyl.use.springboot.requestparam.controller.ControllerHelper.createResponseMapObject;

/**
 * <code>@RequestParam</code> 这个注解用来绑定单个请求数据，既可以是url中的参数，也可以是表单提交的参数和上传的文件。
 */
@Slf4j
@RestController
@RequestMapping(value = "/requestparam")
public class RequestParamDemoController {


    /**
     *
     * <pre>
     *     接收String类型的参数
     * GET /requestparam/demo1?user=xxxx&password=yyyy&rolsList=admin,teller
     * 取user=xxxx
     * 取password=yyyy
     * 取roleList=role1,role2.... 如果不传roleList，则默认为 admin
     * </pre>
     *
     * @param userName
     * @param password
     * @param roles    用户角色，不传则默认为 admin
     * @return
     */
    @GetMapping("/demo1")
    public Map<String, Object> demo1(
            @RequestParam("user") String userName,
            @RequestParam String password,
            @RequestParam(value = "roleList", defaultValue = "admin") String roles) {
        Map<String, Object> back = createResponseMapObject();
        back.put("userName", userName);
        back.put("password", password);
        back.put("roles", roles);
        return back;
    }

    /**
     * 使用<code>@RequestParam</code>接收日期类型的参数
     * @param birthDay 日期类型yyyy-MM-dd
     * @param retTime time类型HH:mm:ss.SSSXXX
     * @param timeStamp 时间戳类型yyyyMMddHHmmssSSS —— 自定义时间格式
     * @return
     */
    @PostMapping("/demo2")
    public Map<String,String> demo2(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date birthDay,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.TIME) Date retTime,
            @RequestParam(required = false) @DateTimeFormat(pattern="yyyyMMddHHmmssSSS") Date timeStamp){

        Map<String,String> back = createResponseMap() ;

        if( null != birthDay ){
            String date = new SimpleDateFormat("yyyy-MM-dd").format(birthDay) ;
            log.info("%s", date);
            back.put("birthDay",date);
        }

        if( null != retTime ){
            String time = new SimpleDateFormat("HH:mm:ss.SSSXXX").format(retTime) ;
            log.info("%s", time );
            back.put("retTime", time);
        }

        if( null != timeStamp ){
            String timestamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(timeStamp );
            log.info("%s", timestamp);
            back.put("timeStamp", timestamp);
        }

        return back ;
    }

    /**
     * 接收数组类型的请求参数
     * @param params
     * @return
     */
    @GetMapping("/demo3")
    public String demo3(@RequestParam(value = "params[]", required = false) String[] params){

        String paramsArray = Arrays.toString(params)  ;
        log.info(paramsArray);
        return paramsArray ;
    }
}
